<?php
namespace Home\Controller;

use Think\Controller;
class ErController extends Controller {


    private $MERID="00000000519067";
    private $KEY="cWtP3fQejNgZ";
    private $TRANS_URL="http://online.atrustpay.com";
    #$TRANS_URL="http://localhost:8081";
    private $SIGN_TYPE="MD5";
    
    public function index(){
    	$token=I('token');
    	$where1['token']=$token;
    	$member_id=M('token')->where($where1)->find()['uid'];
    	$arr['member_id']=$member_id;
    	$arr['money']=I('price');
    	$price=100*I('price');
    	$arr['status']=0;
    	$arr['type']=1;
    	$arr['add_time']=time();
    	
            $data = [
                'versionId'=>1.0,
                'orderAmount'=>$price,
                'orderDate'=>date('YmdHis'),
                'currency'=>'RMB',
                'transType'=>008,
                'asynNotifyUrl'=>'http://dz.money654.com/Home/Return/paybyer',//异步
                'synNotifyUrl'=>'http://dz.money654.com/Home/Return/paybyer',//同步
                'signType'=>$this->SIGN_TYPE,
                'merId'=>$this->MERID,
                'prdOrdNo'=>rand(00000000000000,99999999999999),
                'payMode'=>'00022',
                'receivableType'=>'D00',
                'prdAmt'=>$price,
                'prdDisUrl'=>'',//商品展示网站,
                'prdName'=>'商品名称',
                'prdShortName'=>'商品简介',
            ];
            $arr['order_num']=$data['prdOrdNo'];
            $r=M('Pay')->add($arr);
            $PAY_GATEWAY = '/payment/ScanPayApply.do';
//             $input = file_get_contents("php://input");
            $input = http_build_query($data);
            $input=urldecode($input);
            $string=$this->signString($input);
            $ch=curl_init();
            curl_setopt($ch,CURLOPT_URL,$this->TRANS_URL.$PAY_GATEWAY);
            curl_setopt($ch,CURLOPT_HEADER,0);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
            $data = curl_exec($ch);
            //dump($data);
            $returnData=json_decode($data,true);
            //dump($returnData);
            $ma= "<img src='http://pan.baidu.com/share/qrcode?w=240&h=240&url=".$returnData['qrcode']."'/>";
            $arr['info']=$ma;
            $arr['data']=$returnData;
            $this->ajaxReturn($arr,'JSONP');
            curl_close($ch);
    }
    function signString($input){
        $pieces = explode("&", $input);
        sort($pieces);
        $string='';
        foreach ($pieces as $value){
            if($value!=''){
                $vlaue1= explode("=", $value);
                if($vlaue1[1]!=''&&$value[1]!=null){
                    $string=$string.$value.'&';
                }
            }
        }
        $string=$string.'key='. $this->KEY;
        $sign=strtoupper(md5($string));
        $string=$string.'&signData='.$sign;
        return $string;
    }
    
    function sign($input){
        $pieces = explode("&", $input);
        sort($pieces);
        $string='';
        foreach ($pieces as $value){
            if($value!=''){
                $vlaue1= explode("=", $value);
                if($vlaue1[1]!=''&&$value[1]!=null){
                    $string=$string.$value.'&';
                }
            }
        }
        $string=$string.'key='. $this->KEY;
        $sign=strtoupper(md5($string));
        return $sign;
    }
}